package com.goods.business.service.imp;

import com.goods.business.converter.DateConvert;
import com.goods.business.mapper.SupplierMapper;
import com.goods.business.service.SupplierService;
import com.goods.common.model.business.Supplier;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.SupplierVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@SuppressWarnings("all")
@Service
public class SupplierServiceImpl implements SupplierService {
    @Autowired
    private SupplierMapper supplierMapper;

    //获取物资来源 带条件查询带分页
    @Override
    public PageVO<SupplierVO> findSupplierList(int pageNum, int pageSize, SupplierVO supplierVOArgs) {
        List<Supplier> supplierList = supplierMapper.findSupplierList(supplierVOArgs.getName(), supplierVOArgs.getContact(), supplierVOArgs.getAddress());
        List<SupplierVO> supplierVOList = supplierList.stream().map(supplier -> {
            SupplierVO supplierVO = new SupplierVO();
            BeanUtils.copyProperties(supplier, supplierVO);
            return supplierVO;
        }).collect(Collectors.toList());
        List<SupplierVO> supplierVOListPage = ListPageUtils.page(supplierVOList, pageSize, pageNum);
        //注意这里是 supplierVOList 的长度 !!!
        return new PageVO<SupplierVO>(supplierVOList.size(), supplierVOListPage);
    }

    //添加物资来源
    @Override
    public void add(SupplierVO supplierVO) {
        Supplier supplier = new Supplier();
        BeanUtils.copyProperties(supplierVO, supplier);
        supplier.setCreateTime(new Date());
        supplier.setModifiedTime(new Date());
        supplierMapper.insert(supplier);
    }

    //根据ID查询物资来源信息
    @Override
    public Supplier selectSupplierById(Long id) {
        return supplierMapper.selectByPrimaryKey(id);
    }

    //更新
    @Override
    public void update(Long id, Map editRuleForm) {
        //因为是先从数据库查的 editRuleForm会含有日期字段  当map-转对象时会转换异常  需要日期转换器
        Supplier supplier = new Supplier();
        //网上搜的转换器 自己又做了封装
        DateConvert.Convert();
        try {
            org.apache.commons.beanutils.BeanUtils.populate(supplier, editRuleForm);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        supplier.setModifiedTime(new Date());
        supplierMapper.updateByPrimaryKeySelective(supplier);
    }

    //删除物资来源信息
    @Override
    public void delete(Long id) {
        supplierMapper.deleteByPrimaryKey(id);
    }

    //获取所有物资来源地址
    @Override
    public List<SupplierVO> findAll() {
        return supplierMapper.selectAll().stream().map(supplier -> {
            SupplierVO supplierVO = new SupplierVO();
            BeanUtils.copyProperties(supplier, supplierVO);
            return supplierVO;
        }).collect(Collectors.toList());
    }
}
